package com.xinshujia.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xinshujia.entity.ThresholdEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * *************************************
 * *   @author: 野狼上校               **
 * *   @since: 2024-07-20 09:21       **
 * *   @version: 1.0                  **
 * *************************************
 * <p>
 * 阈值数据持久化接口
 */
@Mapper
public interface ThresholdDao extends BaseMapper<ThresholdEntity> {
    /**
     * 分页查询所有设备的阈值
     */
    @Select({
            "<script>",
            "SELECT t1.*",
            "FROM blade_threshold t1",
            "  INNER JOIN (",
            "SELECT device_guid, MAX(create_time) AS max_create_time",
            "  FROM blade_threshold",
            " GROUP BY device_guid",
            " ) t2 ON t1.device_guid = t2.device_guid AND t1.create_time = t2.max_create_time LIMIT #{offset},#{pageSize};",
            "</script>"
    })
    List<ThresholdEntity> findThresholdsByPage(@Param("offset") long offset, @Param("pageSize") long pageSize);

    /**
     * 根据deviceGuid查询该设备的阈值详情
     */
    @Select("SELECT * FROM blade_threshold WHERE device_guid = #{deviceGuid} ORDER BY create_time DESC LIMIT 0,1;")
    ThresholdEntity findThresholdByDeviceGuid(@Param("deviceGuid") String deviceGuid);
}
